import {BasePage, BasePageData} from "../common/core/BasePage"
import {field} from "../../modules/coreModule/data/DataLoader";
import {page, pageFunc} from "../common/PageBuilder";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {UserInterface} from "../../modules/userModule/UserCommon";
import {post} from "../../modules/coreModule/CoreCommon";
import CustomEvent = WechatMiniprogram.CustomEvent;
import {input} from "../pubTask/PubTaskPage";
import {PayInterface} from "../../modules/payModule/PayCommon";
import {UserPage} from "../common/partPages/UserPage";
import {alertMgr} from "../../modules/coreModule/managers/AlertManager";
import {pageMgr} from "../../modules/coreModule/managers/PageManager";

class Data extends BasePageData {

	@field
	value: number = 0;
	@field
	bankCard: string = "";
	@field
	alipay: string = "";
	@field
	balance: string = "0.00";
}

@page("withdraw", "提现")
export class WithdrawPage extends BasePage<Data> {

	public data = new Data();

	public userPage: UserPage = new UserPage();

	onShow() {
		super.onShow();
		this.loadData().then()
	}

	@waitForLogin
	private async loadData() {
		const res = await UserInterface.GetStat();
		res.balance = res.balance.toFixed(2);
		return this.setData({...res});
	}

	@pageFunc
	@input('value', Number)
	public onValueInput(e: CustomEvent) { }

	@pageFunc // TODO: 银行卡现在是作为姓名使用
	@input('bankCard')
	public onBankCardChange(e: CustomEvent<any>) { }

	@pageFunc
	@input('alipay')
	public onAlipayInput(e: CustomEvent<any>) { }

	@pageFunc
	public async withdrawMoney() {
		if (this.data.value > Number(this.data.balance))
			await alertMgr().showToast("提现金额不能大于当前余额", "error");
		else {
			await PayInterface.ApplyWithdraw({
				value: Math.round(this.data.value * 100),
				alipay: this.data.alipay || this.userPage.userInfo.identification.alipay,
				bankcard: this.data.bankCard || this.userPage.userInfo.identification.bankCard,
			})
			await alertMgr().showToast("成功发起提现申请", "success");
			await pageMgr().pop();
		}
	}
}

